from Products.Archetypes.public import listTypes
from Products.Archetypes.Extensions.utils import installTypes, install_subskin
from Products.CMFCore.utils import getToolByName
from Products.ProjectProgress.config import PROJECTNAME, GLOBALS, STYLESHEETS

from StringIO import StringIO


left_portlets = None #['here/portlet_navigation/macros/portlet',]
right_portlets = None


def install(self):
    """ Main install function """
    out = StringIO()

    ## Add Portlets
    #install_portlets(self, out)

    ## Configure and install new archetypes
    configureTypes(self, out)

    ## Register stylesheets
    registerResources(self, out, 'portal_css', STYLESHEETS)

    install_subskin(self, out, GLOBALS)

    print >>out, "Successfully installed %s." % PROJECTNAME
    return out.getvalue()

def configureTypes(self, out):
    """ Register new types and configure them """

    installTypes(self, out, listTypes(PROJECTNAME), PROJECTNAME)

    self.portal_factory.manage_setPortalFactoryTypes(None,['Project','Taskfolder','Task', 'TaskExtra'])


def install_portlets(self, out):
    """ Install the portlets """
    
    utool = getToolByName(self, 'portal_url')
    portal = utool.getPortalObject()
    #portlets = list(portal.getProperty('left_slots'))

    portlets_left = []
    portlets_right = []
    
    if (left_portlets != None):   
    	for portlet in left_portlets:
    		if portlet not in portlets_left:
    			portlets_left.append(portlet)
              
    ## Top placement: portlets.insert(0,portlet)
    ## Buttom placement: portlets.append(portlet)
    
    if (right_portlets != None):
    	for portlet in right_portlets:
    		if portlet not in portlets_right:
    			portlets_right.append(portlet)
   
    portal._updateProperty('left_slots', portlets_left)
    portal._updateProperty('right_slots', portlets_right)
    
    	
    
 
    print >>out, 'Portlets installed.\n'

def registerResources(self, out, toolname, resources):
    """ Register site resources """

    tool = getToolByName(self, toolname)
    existing = tool.getResourceIds()
    cook = False
    for resource in resources:
        if not resource['id'] in existing:
            # register additional resource in the specified registry...
            if toolname == "portal_css":
                tool.registerStylesheet(**resource)
            if toolname == "portal_javascripts":
                tool.registerScript(**resource)
            print >> out, "Added %s to %s." % (resource['id'], tool)
        else:
            # ...or update existing one
            parameters = tool.getResource(resource['id'])._data
            for key in [k for k in resource.keys() if k != 'id']:
                originalkey = 'original_'+key
                original = parameters.get(originalkey)
                if not original:
                    parameters[originalkey] = parameters[key]
                parameters[key] = resource[key]
                print >> out, "Updated %s in %s." % (resource['id'], tool)
                cook = True
    if cook:
        tool.cookResources()
    print >> out, "Successfuly Installed/Updated resources in %s." % tool

